package com.ruoyi.Awards.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.Awards.mapper.AwardsMapper;
import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.sun.org.glassfish.external.amx.AMX;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Awards.domain.Awards;
import com.ruoyi.Awards.service.IAwardsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目表Controller
 * 
 * @author ruoyi
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/Awards/awards")
public class AwardsController extends BaseController
{
    @Autowired
    private IAwardsService awardsService;

    @Autowired
    private AwardsMapper awardsMapper;

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    /**
     * 查询项目表列表
     */
//    @PreAuthorize("@ss.hasPermi('Awards:awards:list')")
    @GetMapping("/list")
    public TableDataInfo list(Awards awards)
    {
        startPage();
        awards.setPointId(getLoginUser().getUsername().equals("admin")? 0 : getLoginUser().getUserId());
        List<Awards> list = awardsService.selectAwardsList(awards);
//        if(list.size() != 0 && getLoginUser().getUser().getRemark().equals("管理员")){
//            for (int i = 0; i < list.size(); i++) {
//                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
//            }
//        }
        return getDataTable(list);
    }

    //查询专家评审项目信息
    @GetMapping("/byExpert")
    public TableDataInfo listByExpert()
    {
        startPage();
        List<Awards> list = awardsMapper.selectByExpert(getLoginUser().getUserId());
        if(list.size() != 0){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出项目表列表
     */
    @PreAuthorize("@ss.hasPermi('Awards:awards:export')")
    @Log(title = "项目表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Awards awards)
    {
        List<Awards> list = awardsService.selectAwardsList(awards);
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        util.exportExcel(response, list, "项目表数据");
    }

    /**
     * 获取项目表详细信息
     */
    @PreAuthorize("@ss.hasPermi('Awards:awards:query')")
    @GetMapping(value = "/{awardId}")
    public AjaxResult getInfo(@PathVariable("awardId") Long awardId)
    {
        return success(awardsService.selectAwardsByAwardId(awardId));
    }

    /**
     * 新增项目表
     */
    @PreAuthorize("@ss.hasPermi('Awards:awards:add')")
    @Log(title = "项目表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Awards awards)
    {
        awards.setPointId(getLoginUser().getUserId());
        return toAjax(awardsService.insertAwards(awards));
    }

    /**
     * 修改项目表
     */
    @PreAuthorize("@ss.hasPermi('Awards:awards:edit')")
    @Log(title = "项目表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Awards awards)
    {
        return toAjax(awardsService.updateAwards(awards));
    }

    /**
     * 删除项目表
     */
    @PreAuthorize("@ss.hasPermi('Awards:awards:remove')")
    @Log(title = "项目表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{awardIds}")
    public AjaxResult remove(@PathVariable Long[] awardIds)
    {
        return toAjax(awardsService.deleteAwardsByAwardIds(awardIds));
    }
}
